package netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 *
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("server read....");
        ByteBuf buf = (ByteBuf) msg;
        byte[] date = new byte[buf.readableBytes()];
        buf.readBytes(date);
        System.out.println("client msg: " + new String(date));
        buf.release();

        System.out.println("send msg to client...");
        String response = "hello client";
        ByteBuf buf1 = ctx.alloc().buffer();
        buf1.writeBytes(response.getBytes());
        ctx.write(buf1);
        ctx.flush();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
}
